<?php
class channelModel extends baseModel{
    protected $table ='cms_class';  //模型表
    //判断分类模型信息
    public function classModel($cid){
        $cid=intval($cid);
        $rel = $this->find('id='.$cid,'mid');
        $tables = model('module')->tables($rel['mid']);
        return $tables;
    }
    //内容管理菜单
    public function leftmenu(){
        $Cookie = new Cookie();
        $language = $Cookie->get('siteid');
        if($language){
            $condition['id'] = intval($language);
            $rel = $this->model->table('cms_language')->field('types,id,name')->where($condition)->find(); 
            if($rel){
                $arr['title'] = '[子站]'.$rel['name'];
                $language_class = ' and language = '.$rel['id'];
            }
        }else{
            $arr['title'] = '内容管理';
        }
        $rel = $this->select('type <= 1 and pid = 0'.$language_class,'id,name');
        if($rel){
             foreach($rel as $rs){
               $arr['list']['<i class="icon">&#xf0da;</i> '.$rs['name']] = url('article/lists',array('id'=>$rs['id']));
             }
        }
        return $arr;
    }
    //栏目分类
    public function ChannelName($cid){
      $condition['id'] = $cid;
      $rel =  $this->find($condition,'name,id');
      return $rel['name'];
    }
    //栏目管理菜单
    public function ChannelRootMenu(){
        $rel = $this->select('type <= 1 and pid = 0','id,name');
        if($rel){
            foreach($rel as $rs){
              $str[$rs['name']] = url('channel/index',array('id'=>$rs['id']));
            }
        }
        return $str;
    }
    //获取根分类
    public function RootClass(){
      $rel = $this->select('pid = 0 and type <= 1','name,id');
      return $rel;
    }
    //分类列表
    public function classList($id=0,$limit) {
        $Cookie = new Cookie();
        $language = $Cookie->get('siteid');
        if($language){
            $language_sql = ' and a.language = '.$language;
        }
        $id = intval($id);
        $sql = "SELECT a.id,a.pid,a.type,a.url,a.name,a.exts_mid,a.plus_mid,a.uptime,a.neworder,a.language,b.id as mid,b.name as model_name FROM ".config('DB_PREFIX').$this->table." as a,".config('DB_PREFIX')."cms_module as b WHERE a.mid=b.id and a.pid = $id".$language_sql." order by a.neworder asc,a.id asc limit ".$limit;
        $data = $this->query($sql);
        return $data;
    }
    //添加网站分类
    public function addClass($data = array()){
        $info = $this->insert($data); //插入模型数据
        if($info){
            return TRUE;
        }else{
            return FALSE;
        }
    }
    //删除网站分类
    public function delClass($fid){
        $subclass = $this->find('pid = '.$fid); //查看是否有子分类
        if($subclass){
            return FALSE;
        }
        $article_table = $this->classModel($fid);//查看是否包含内容
        $rel = $this->model->table($article_table)->where('cid='.$fid)->field('id')->find();;
        if($rel){
            return FALSE;
        }
        $info = $this->model->table($this->table)->where('id='.$fid)->field('id,exts_mid')->find(); 
        if($info['exts_mid']){
            $exts_tables = model('module')->tables($info['exts_mid']);
            $this->model->table($exts_tables)->where('cid='.$info['id'])->delete();
            $this->model->table($exts_tables.'_data')->where('cid='.$info['id'])->delete();
        }
        $result = $this->delete('id = '.$fid);
        if($result){
            return TRUE;
        }else{
            return FALSE;
        }
    }
    //编辑网站分类
    public function editClass($fid,$mid = null,$postinput = array()){
        $data = array();
        $data['type'] = $postinput['type'];
        $data['name'] = $postinput['name'];
        $data['language'] = $postinput['language'];
        $data['url'] = $postinput['url'];
        $data['locks'] = $postinput['locks'];
        $data['tpl_channel'] = $postinput['tpl_channel'];
        $data['tpl_list'] = $postinput['tpl_list'];
        $data['tpl_content'] = $postinput['tpl_content'];
        $data['tpl_plus'] = $postinput['tpl_plus'];
        $data['uptime'] = $postinput['uptime']; 
        $condition['id'] = $fid;
        $info = $this->update($condition,$data); //插入模型数据
        if($info && $fid && $mid){
            $fieids = model('field')->select('mid='.$mid,'fields,tbdata');
            if($fieids){
                foreach ($fieids as $key => $value) {
                  if($value['tbdata']){
                      $extdata[$value['fields']] = $postinput[$value['fields']]; //副表
                  }else{
                      $ext[$value['fields']] = $postinput[$value['fields']]; //主表
                  }
                } 
            }
            //入库
            $this->table = model('module')->tables($mid);
            $rel = $this->find('cid='.$fid);
            if($rel){
                $condition['id'] = $rel['id'];
                $ext['cid'] = $fid;
                $result_ext = $this->update($condition,$ext);
                $this->table = $this->table.'_data';
                $extdata['cid'] = $fid;;
                $result_extdata = $this->update($condition,$extdata);
                return TRUE;
            }else{
                $ext['cid'] = $fid;
                $this->insert($ext);
                $extdata['id'] = mysql_insert_id();
                $extdata['cid'] = $fid;
                $this->table = $this->table.'_data';
                $this->insert($extdata);
                return TRUE;
            }
        }
        if($info){
          return TRUE;
        }else{
          return FALSE;
        }
    }
    //快捷编辑网站内容
    public function articleedit($fid,$mid = null,$postinput = array()){
        $fieids = model('field')->select('mid='.$mid,'fields,tbdata');
        foreach ($fieids as $value) {
          if($value['tbdata']){
              $extdata[$value['fields']] = $postinput[$value['fields']];//副表
          }else{
              $ext[$value['fields']] = $postinput[$value['fields']];//主表
          }
        }
        $tables = model('module')->tables($mid);
        $rel = $this->model->table($tables)->where('cid='.$fid)->find();
        //入库
        $condition['id']=$rel['id'];
        $rel = $this->model->table($tables)->where($condition)->data($ext)->update();
        if($rel){
          $extdata['cid'] = $fid;
          $info = $this->model->table($tables.'_data')->where($condition)->data($extdata)->update();
        }
        if($info){
          return TRUE;
        }else{
          return FALSE;
        }  
    }
    //编辑分类读取扩展内容
    public function editClassExt($cid,$mid){
        $mid=intval($mid);
        $this->table = model('module')->tables($mid);
        $info = array();
        $condition['cid']=$cid;
        $info = $this->find($condition);
        if($info){
            $ext_conn['id'] = $info['id'];
            $info = postinput($info,array('id','cid','uptime'));
            $this->table = $this->table.'_data';
            $infoplus = $this->find($ext_conn);
            $infoplus = postinput($infoplus,array('id','cid'));
            if(!$infoplus){
              $infoplus = array();
            }
            return array_merge($info,$infoplus);
        }
    }
    //当前路径
    public function flpath($pid = 0,$url = null) {
        $condition['id'] = intval($pid);
        $result = $this->find($condition,'id,pid,name');
        if ($result) {
            $Str = $this->flpath($result['pid'],$url);
            if($url){
                $Str .= '<a href="' . url(''.$url.'',array(id=>$result['id'])).'">' . $result['name'] . '</a>';
            }else{
                $Str .= '<a href="###">' . $result['name'] . '</a>';
            }
        }
        return $Str;
    }
    //#######################################
    //获取分类树，$id，分类id,$id=0，获取所有分类结构树
    public function getClass($id=0) {
        //$data = $this->select('','id,pid,name'); //第一种 
        $data = $this->getAllClass($id); //第二种
        $cat = new Category(array('id', 'pid', 'name', 'cname'));
        return $cat->getTree($data, $id);
    }
   //获取指定ID下全部目录信息
   public function getAllClass($fid){
       $rel = $this->select('pid='.$fid,'id,pid,name,type,uptime');
        if($rel){
            foreach($rel as $rs){
                $str = $this->getAllClass($rs['id']);
                if($str){
                    foreach($str as $vo){
                       $rel[] = $vo;
                    }
                }
            }
        }
        return $rel;
   }
   //#######################################
   //递归搜当前目录下所有的下级目录ID（无限层型目录）
   public function AllChildClass($pid){
       $data = $this->getAllClass($pid);
       $str = $pid;
       if($data){
           foreach($data as $rs){
               $str.= ','.$rs['id'];
           }
       }
       return $str;
   }
   //栏目下拉菜单
   public function CreateClassTree($rootid,$cid,$topid = null){
        $rootid = intval($rootid);
        //主栏目
        if(!$topid){
          echo '<li>';
            $rootclass = $this->find('type <= 1 and id = '.$rootid, 'id,name,type');
            if($rootclass['type']){
              $rootdisabled = ' disabled="disabled"';
            }else{
              if($cid == $rootclass['id']){
                $rootradioSelected =' checked="checked"';
              }
            }
            echo '<input class="radio"'.$rootdisabled.$rootradioSelected.' type="radio" name="cid" value="'.$rootclass['id'].'" /> '.$rootclass['name'];
        }
        //包含所有自栏目
        $rel = $this->select('type <= 1 and pid = '.$rootid, 'id,name,type');
        if($rel){
             echo "<ul>";
             foreach($rel as $rs){
                echo '<li>';
                if(!$rs['type']){
                  echo '<input class="radio"';
                  if($cid == $rs['id']){
                    echo ' checked="checked"';
                  }
                  echo ' type="radio" name="cid" value="'.$rs['id'].'" />';
                }
                echo $rs['name'];
                $this->CreateClassTree($rs['id'],$cid,1);
                echo '</li>';
             }
             echo "</ul>";
        }
        if(!$topid){
          echo "</li>";
        }
   }
//栏目下拉菜单
   public function ClassTree($rootid,$topid = 1){
        $rootid = intval($rootid);
        //包含所有自栏目
        $rel = $this->select('type <= 1 and pid = '.$rootid, 'id,name,type');
        if($rel){
             echo "<ul>";
             foreach($rel as $rs){
                echo '<li> <a href="'.url('article/lists',array('id'=>$rs['id'])).'" class="p5">'.$rs['name']."</a>";
                $this->ClassTree($rs['id'],0);
                echo '</li>';
             }
             echo "</ul>";
        }
   }
   //##########################################################
   //前台栏目标签算法
   public function ChannelLabel($get = array(),$cid = 0,$off = 1){
        //判断是否锁定和当前栏目
        $pid .= $get['es_class'].' = '.intval($cid);
        //SQL条件
        if(!empty($get['es_where'])){
            $where = ' and '.$get['es_where'];
        }
        $order = $get['es_order'];
        if(empty($get['es_order'])){
            $order = 'id desc';
        }
        if($off){ //1代表可以显示
          $locks = 'locks = 1 and ';
        }
        $info = $this->select($locks.$pid.$where,null,$order);
        if($info){ 
            foreach ($info as $key => $value) {
                if($value['exts_mid']){
                    $mid = $value['exts_mid'];
                    $tables = model('module')->tables($mid);
                    $condition['cid'] = $value['id'];
                    $rel = $this->model->table($tables)->where($condition)->find();
                    $rel_ext = $this->model->table($tables.'_data')->where($condition)->find();
                    if($rel && $rel_ext){
                      $ext = array_merge($rel_ext,$rel);
                      $str = postinput($ext,array('id','cid','uptime'));
                    }
                }
                $del = array('mid','exts_mid','neworder','type','rootid','url','pid');
                if(is_array($str)){
                    $result[] = postinput(array_merge($value,$str),$del);   
                }else{
                    $result[] = postinput($value,$del);
                }
            }
        }
        return $result;
    }
}